<?php

  /**
   * @package JooS_PHPackager_Logger
   */
  class JooS_PHPackager_Logger {
    private static $_instance = null;

    public static function Observer_beforeAddPackages(JooS_Event_Phpackager_Add_Before $event) {
      self::newInstance()
        ->log("Packages installation started");
    }

    public static function Observer_afterAddPackages(JooS_Event_Phpackager_Add_After $event) {
      self::newInstance()
        ->log("Packages installation complete");
    }

    public static function Observer_beforeRemovePackage(JooS_Event_Phpackager_Remove_Before $event) {
      self::newInstance()
        ->log("Package removing started");
    }

    public static function Observer_afterRemovePackage(JooS_Event_Phpackager_Remove_After $event) {
      $result = $event->result;

      self::newInstance()
        ->log("Package removing " . ($result ? "complete" : "failed"));
    }

    private static function newInstance() {
      if (self::$_instance === null)
        self::$_instance = new self();
      return self::$_instance;
    }

    protected function __construct() {
      require_once "JooS/PHPackager/Manager.php";

      require_once "JooS/Event/Phpackager/Package/Instance.php";
      require_once "JooS/Event/Phpackager/Package/Installed.php";
      require_once "JooS/Event/Phpackager/Package/Upgraded.php";
      require_once "JooS/Event/Phpackager/Package/Commited.php";

      JooS_Event_Phpackager_Package_Instance::getInstance()
        ->attach(array($this, "Observer_onPackageNewInstance"));

      JooS_Event_Phpackager_Package_Installed::getInstance()
        ->attach(array($this, "Observer_onPackageInstalled"));

      JooS_Event_Phpackager_Package_Upgraded::getInstance()
        ->attach(array($this, "Observer_onPackageUpgraded"));

      JooS_Event_Phpackager_Package_Commited::getInstance()
        ->attach(array($this, "Observer_onPackageCommited"));
    }

    protected function log($message) {
      echo $message . "\n";
    }

    public function Observer_onPackageNewInstance(JooS_Event_Phpackager_Package_Instance $event) {
      $package = $event->package;
      $this->log( "New package instance created: " . $package->getName() . " v" . $package->getVersion() );
    }

    public function Observer_onPackageInstalled(JooS_Event_Phpackager_Package_Installed $event) {
      $package = $event->package;
      $this->log( "Package " . $package->getName() . " v" . $package->getVersion() . " installed" );
    }

    public function Observer_onPackageUpgraded(JooS_Event_Phpackager_Package_Upgraded $event) {
      $NEW_package = $event->NEW_package;
      $OLD_package = $event->OLD_package;
      $this->log( "Package " . $NEW_package->getName() . " upgraded from v" . $OLD_package->getVersion() . " to " . $NEW_package->getVersion() );
    }

    public function Observer_onPackageCommited(JooS_Event_Phpackager_Package_Commited $event) {
      $package = $event->package;
      $this->log( "Package " . $package->getName() . " v" . $package->getVersion() . " commited into system" );
    }
  }
